package com.us.usws.support.servlet;

import java.io.IOException;
import java.io.InputStream;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebInitParam;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.iwsh.core.common.GlobalConfig;
import net.iwsh.core.db.connection.ConnectionFactory;
import net.iwsh.core.utils.ResourceHelper;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 * Platform init servlet
 */
@WebServlet(name = "SiteInitServlet", urlPatterns = { "/siteInit" }, initParams = { @WebInitParam(name = "GlobalConfigPath", value = "/global.conf.xml") }, loadOnStartup = 1)
public class SiteInitServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	private Log log = LogFactory.getFactory().getInstance(this.getClass().getName());

	/* (non-Javadoc)
	 * @see javax.servlet.http.HttpServlet#doGet(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
	 */
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		String act = req.getParameter("act");
		if ("refresh".equalsIgnoreCase(act)) {
			buildConfig();
			resp.getWriter().println("Refresh done.");
		} else {
			resp.getWriter().println("Site initialization was already done!");
		}
	}

	/**
	 * Place this filter into service.
	 *
	 * @throws javax.servlet.ServletException if a servlet error occurs
	 */
	public void init() throws ServletException {
		//initial global config
		buildConfig();

		//add guice injectors
		//GuiceInjectorFactory.lookup();

		//load DataSet conf files
		//log.info("Loading DataSet configurations...");
		//DataSetConfLoader.getInstance().refresh();

	}

	/**
	 * build config
	 *
	 * @param resource
	 */
	private void buildConfig() {
		String resource = this.getInitParameter("GlobalConfigPath");

		InputStream is = null;
		try {
			is = ResourceHelper.getResourceAsStream(resource);
		} catch (Exception e) {
			log.trace("Could not find [" + resource + "] in general path, try to seek it in current package/path...");
			is = this.getClass().getResourceAsStream(resource);
			if (is == null) {
				log.trace("Could not find [" + resource + "] in package/path, try to seek it in classes path...");
				resource = "/WEB-INF/classes" + resource;

				try {
					String realPath = this.getServletContext().getRealPath(resource);
					is = this.getServletContext().getResourceAsStream(realPath);
				} catch (Exception e1) {
					log.trace("Could not find [" + resource + "] in all locations.");
				}
			}
		}

		//load global config
		if (is != null) {
			log.info("Loading global configurations...");
			GlobalConfig.buildConfig(is);
		} else {
			log.fatal("Cannot get global configurations!");
		}

	}

	/* (non-Javadoc)
	 * @see javax.servlet.GenericServlet#destroy()
	 */
	@Override
	public void destroy() {
		super.destroy();
		ConnectionFactory.shutdown();
		//GuiceInjectorFactory.dispose();
	}

}